
package linuxbox.util;

import java.io.*;
import java.sql.*;
import java.sql.Date;
import java.util.*;

import javax.servlet.*;
import javax.servlet.http.*;

import linuxbox.bean.*;
import linuxbox.common.*;
import linuxbox.bbs.bean.*;
import linuxbox.exception.*;

public class WebTools { 
	// Constants for global use
	public static final int UNKNOWN_NUMBER = -1;
	public static final String UNKNOWN_STRING = "unknown";
	
	private final static char[] ACHTML = {'&', '"', '<', '>', '\n', ' ', '\r'};
 	private final static String[] ASTRHTML = {
 		"&amp;", "&quot;", "&lt;", "&gt;", "", "&nbsp;", "<br>"
 	};
 
 	public static String encodeHtml(String s) {
  		if (s == null)
   			return  "";

		if (s.indexOf(ACHTML[0]) != -1 || s.indexOf(ACHTML[1]) != -1 ||
			s.indexOf(ACHTML[2]) != -1 || s.indexOf(ACHTML[3]) != -1 ||
			s.indexOf(ACHTML[4]) != -1 || s.indexOf(ACHTML[5]) != -1 ||
			s.indexOf(ACHTML[6]) != -1 ) 
			s = replace(s, ACHTML, ASTRHTML);

  		return s;
	}

	private static String replace(String s, char[] ac, String[] astr) {
  		StringBuffer sb = new StringBuffer();

  		for (int i=0; i<s.length(); i++) {
   			char c = s.charAt(i);
   			boolean blReplaced = false;
   
   			for (int j=0; j<ac.length; j++) {
    			if (c == ac[j]) {
     				sb.append(astr[j]);
     				blReplaced = true;
     				break;
    			}
   			}

   			if (! blReplaced)
    			sb.append(c);
  		}

  		return sb.toString();
 	}

	public static BoxSession getBoxSession(HttpServletRequest req) {
		HttpSession hs = req.getSession(false);
		if (hs == null)
			return null;
		else 	
			return (BoxSession)hs.getAttribute("BoxSession");
	}
	
	public static BoxSession getBoxSession(HttpSession session) {
		return (BoxSession)session.getAttribute("BoxSession");
	}

	public static Enumeration sortMsgList(Enumeration originList, DbAdapter db) throws Exception {
		//the originList should order by iIndentLevel
		BbsMsgKey key = null;
		BbsMsgBean bean = null;

        Vector vctCurrent = null;
	    
		try {
            if (originList != null) {
                vctCurrent = new Vector();
            	
	            while (originList.hasMoreElements()) {
	                key = (BbsMsgKey)originList.nextElement();

                	if (db == null) 
                		bean = new BbsMsgBean(key);
                	else
						bean = new BbsMsgBean(key, db);
	                	
				    int iIndentLevel = bean.getIndLvl();
	
					if (iIndentLevel <= 1) { 
	                	vctCurrent.addElement(bean);
	                } else {
	                    /*Enumeration e = vctCurrent.elements();
	                    while (e.hasMoreElements()) 
	                    {
		                    BbsMsgBean tmpbean = (BbsMsgBean)e.nextElement();
	
				            if (bean.getParentID() == tmpbean.getMsgID())
				            {
					            int i = vctCurrent.indexOf(tmpbean);
					            vctCurrent.insertElementAt(bean, i + 1);
					            break;
							}
			            }*/
			            
			            int len = vctCurrent.size();
			            
			            boolean found = false;
			            for (int i = len - 1; i >= 0; i--) {
			            	BbsMsgBean tmpbean = (BbsMsgBean)vctCurrent.elementAt(i);
			            	
			            	if (bean.getParentID() == tmpbean.getParentID() ||
			            		bean.getParentID() == tmpbean.getMsgID()) {
			            		if (i == len - 1)
			            			vctCurrent.addElement(bean);
			            		else
			            			vctCurrent.insertElementAt(bean, i + 1);
			            		
			            		found = true;
			            		break;
			            	}
			            }

		            	if (!found) {	
			            	vctCurrent.addElement(bean);
			            }		
	                }
				}
			}
		} catch(Exception e) {
			throw e;
		}        
        
        return vctCurrent.elements();
	}

	/*public static Enumeration sortNext20(Enumeration originList) throws Exception
	{
		//the originList should order by iIndentLevel
		BbsMsgKey key = null;

        Vector vctCurrent = null;
	    Vector result = null;
		Enumeration enum = null;
		Enumeration ret = null;
	    
		try
		{
            if (originList != null)
            {
                vctCurrent = new Vector();
            	result = new Vector();
            	
	            long lCurrentTopicID = 0;
	            if (originList.hasMoreElements())
	            {
	                key = (BbsMsgKey)originList.nextElement();
				    lCurrentTopicID = key.m_lTopicID;
				    vctCurrent.addElement(key);
				}	            	
	            
	            while (originList.hasMoreElements()) 
	            {
	                key = (BbsMsgKey)originList.nextElement();
	                
				    if ( lCurrentTopicID == key.m_lTopicID)
				    {
                		vctCurrent.addElement(key);
					}
					else
					{
						enum = vctCurrent.elements();
						ret = sortMsgList(enum);
												
		            	while (ret.hasMoreElements())
		            	{
		            		BbsMsgBean bean = (BbsMsgBean)ret.nextElement();
		                	result.addElement(bean);
		                }
		                
		                vctCurrent.removeAllElements();
		                lCurrentTopicID = key.m_lTopicID;
		                vctCurrent.addElement(key);
	                }
				}
				enum = vctCurrent.elements();
				ret = sortMsgList(enum);
										
            	while (ret.hasMoreElements())
            	{
            		BbsMsgBean bean = (BbsMsgBean)ret.nextElement();
                	result.addElement(bean);
                }
			}
		}
		catch(Exception e)
		{
			throw e;
		}        
        
        return result.elements();
	}*/
}
